package com.enterpriseDigitalPlatform.controller.renshi.attendance;

import com.enterpriseDigitalPlatform.dto.JsonRes;
import com.enterpriseDigitalPlatform.dto.PageRes;
import com.enterpriseDigitalPlatform.exception.ServiceException;
import com.enterpriseDigitalPlatform.util.RequiredCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/renshi-attendance")
public class AttendanceC {
    private AttendanceDao attendanceDao;

    @Autowired
    public AttendanceC(AttendanceDao attendanceDao) {
        this.attendanceDao = attendanceDao;
    }

    @RequestMapping("/addData")
    public Object addData(@RequestBody AttendanceDTO attendanceDTO) {
        if (RequiredCheckUtil.checkInt(attendanceDTO.getDeptid())) {
            throw new ServiceException("部门id不能为空");
        }
        if (RequiredCheckUtil.checkInt(attendanceDTO.getStaffId())) {
            throw new ServiceException("员工id不能为空");
        }
        if (RequiredCheckUtil.checkInt(attendanceDTO.getTime())) {
            throw new ServiceException("打卡时间不能为空");
        }
        if (RequiredCheckUtil.checkString(attendanceDTO.getLng())) {
            throw new ServiceException("经度不能为空");
        }
        if (RequiredCheckUtil.checkString(attendanceDTO.getLat())) {
            throw new ServiceException("纬度不能为空");
        }
        // 加锁
        // 判断最近的记录状态
        // 上班 修改为下班
        // 下班 创建新上班记录
        int row = 0;
        return new JsonRes<>(row);
    }

    @PostMapping("/getList")
    public Object getList(@RequestBody AttendanceDTO attendanceDTO) {
        if (RequiredCheckUtil.checkInt(attendanceDTO.getPage())) {
            attendanceDTO.setPage(1);
        }
        if (RequiredCheckUtil.checkInt(attendanceDTO.getPageSize())) {
            attendanceDTO.setPageSize(10);
        }
        PageRes<Attendance> pageRes = attendanceDao.getList(attendanceDTO);
        return new JsonRes<>(pageRes);
    }

    @PostMapping("/getData")
    public Object getData(@RequestBody AttendanceDTO attendanceDTO) {
        if (RequiredCheckUtil.checkInt(attendanceDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        PageRes<Attendance> pageRes = attendanceDao.getList(attendanceDTO);
        Attendance attendance = new Attendance();
        List<Attendance> list = pageRes.getData();
        if (!list.isEmpty()) {
            attendance = list.get(0);
        }
        return new JsonRes<>(attendance);
    }

}
